from enum import StrEnum

from sqlalchemy import func
from sqlalchemy.dialects.postgresql import JSONB
from models.engine import db
from models.types import StringUUID

class CommentStatus(StrEnum):
    """评论状态"""
    DESPLAY = "D" # 显示
    PENDING = "P" # 等待审核
    NOT_APPROVED = "N" # 未通过审核

class MFavoriteProdcut(db.Model):
    """收藏商品"""
    __tablename__ = "sp_prod_favorite"
    __table_args__ = (
        db.PrimaryKeyConstraint("id", name="sp_prod_favorite_pkey"),
        db.Index("sp_prod_favorite_account_id_idx", "created_by"),
        db.Index("sp_prod_favorite_prod_id_idx", "prod_id"),
    )
    id = db.Column(db.BigInteger, primary_key=True, autoincrement=True,comment="ID")
    prod_id = db.Column(StringUUID, nullable=False,comment="产品ID")
    created_by = db.Column(StringUUID, nullable=False,comment="收藏人")
    created_at = db.Column(db.TIMESTAMP(timezone=False),server_default=func.current_timestamp(), nullable=False,comment="创建时间")

class MProductComment(db.Model):
    """产品评论"""
    __tablename__ = "sp_prod_comm"
    __table_args__ = (
        db.PrimaryKeyConstraint("id", name="sp_product_comment_pkey"),
        db.Index("sp_product_comment_prod_id_idx", "prod_id"),
        db.Index("sp_product_comment_account_id_idx", "account_id"),
        db.Index("sp_product_comment_order_item_idx", "order_item_id"),
    )
    id = db.Column(db.BigInteger, primary_key=True, autoincrement=True,comment="ID")
    prod_id = db.Column(StringUUID, nullable=False,comment="产品ID")
    order_item_id = db.Column(db.BigInteger, nullable=False,comment="订单项ID")
    account_id = db.Column(StringUUID, nullable=False,comment="账号ID")
    content = db.Column(db.String(512), nullable=False,comment="评论内容")
    reply = db.Column(db.String(512), nullable=True,comment="回复内容")
    comm_at = db.Column(db.TIMESTAMP(timezone=False),server_default=func.current_timestamp(), nullable=False,comment="评论时间")
    reply_at = db.Column(db.TIMESTAMP(timezone=False), nullable=True,comment="回复时间")
    reply_status = db.Column(db.Boolean, nullable=False,server_default=db.text("false"),comment="回复状态")
    ip_addr = db.Column(db.String(32), nullable=False,comment="IP地址")
    score = db.Column(db.BigInteger, nullable=True,server_default=db.text('0'),comment="评分")
    useful_counts = db.Column(db.Integer, nullable=True,server_default=db.text('0'),comment="有用数")
    pics = db.Column(JSONB, nullable=True,server_default=db.text("'{}'::jsonb"),comment="晒图列表")
    is_anonymous = db.Column(db.Boolean, nullable=False,server_default=db.text("false"),comment="是否匿名")
    status = db.Column(db.String(1),server_default=db.text("'D'::character varying"), nullable=False,comment="状态")
    evaluate = db.Column(db.String(1), nullable=True,comment="评价")

    @property
    def pic_file_ids(self)->list[str]:
        if self.pics:
            return self.pics.get("pic_file_ids",[])
        return []
    
    @pic_file_ids.setter
    def pic_file_ids(self,value:list[str]):
        if self.pics:
            self.pics["pic_file_ids"] = value
        else:
            self.pics = {"pic_file_ids":value}

class MUserProductCollection(db.Model):
    __tablename__ = "sp_user_collection"
    __table_args__ = (
        db.PrimaryKeyConstraint("id", name="sp_user_collection_pkey"),
        db.Index("sp_user_collection_account_id_idx", "account_id"),
        db.Index("sp_user_collection_prod_id_idx", "prod_id"),
    )
    id = db.Column(db.BigInteger, primary_key=True, autoincrement=True,comment="ID")
    account_id = db.Column(StringUUID, nullable=False,comment="账号ID")
    prod_id = db.Column(StringUUID, nullable=False,comment="产品ID")
    created_at = db.Column(db.TIMESTAMP(timezone=False),server_default=func.current_timestamp(), nullable=False,comment="创建时间")
    
class MProductTag(db.Model):
    """
    商品标签/分组
    """
    __tablename__ = "sp_prod_tag"
    __table_args__ = (
        db.PrimaryKeyConstraint("id", name="sp_prod_tag_pkey"),
        db.Index("sp_prod_tag_shop_id_idx", "shop_id"),
        db.Index("sp_prod_tag_system_idx", "is_system"),
    )
    id = db.Column(db.BigInteger, primary_key=True, autoincrement=True,comment="ID")
    tenant_id = db.Column(StringUUID, nullable=False,comment="租户ID")
    title = db.Column(db.String(36), nullable=False,comment="标题")
    shop_id = db.Column(StringUUID, nullable=False,comment="店铺ID")
    status = db.Column(db.Boolean(),server_default=db.text("true"), nullable=False,comment="状态:true:正常,false:禁用")
    is_system = db.Column(db.Boolean(),server_default=db.text("false"), nullable=False,comment="是否系统标签:true:是,false:否")
    prod_count = db.Column(db.BigInteger,server_default=db.text('0'), nullable=False,comment="商品数量")
    list_style = db.Column(db.SmallInteger,server_default=db.text('0'), nullable=False,comment="列表样式:0:一列一个,1:一列2个，2：一列3个")
    seq = db.Column(db.Integer, nullable=False,server_default=db.text('0'),comment="排序")
    created_at = db.Column(db.TIMESTAMP(timezone=False),server_default=func.current_timestamp(), nullable=False,comment="创建时间")
    created_by = db.Column(StringUUID, nullable=False,comment="创建人")
    updated_at = db.Column(db.TIMESTAMP(timezone=False), nullable=True,comment="更新时间")
    deleted_at = db.Column(db.TIMESTAMP(timezone=False), nullable=True,comment="删除时间")

class MProductTagProduct(db.Model):
    """
    商品标签关联表
    """
    __tablename__ = "sp_product_tag_reference"
    __table_args__ = (
        db.PrimaryKeyConstraint("id", name="sp_product_tag_reference_pkey"),
        db.Index('sp_product_tag_reference_tag_id_idx', 'tag_id'),
        db.Index('sp_product_tag_reference_product_id_idx', 'prod_id'),
    )
    id = db.Column(db.BigInteger, primary_key=True, autoincrement=True)
    tenant_id = db.Column(StringUUID, nullable=False,comment="租户ID")
    shop_id = db.Column(StringUUID, nullable=True,comment="店铺ID")
    tag_id = db.Column(db.BigInteger, nullable=False,comment="标签ID")
    prod_id = db.Column(StringUUID, nullable=False,comment="商品ID")
    status = db.Column(db.Boolean(),server_default=db.text("true"), nullable=False,comment="状态:true:正常,false:禁用")
    created_at = db.Column(db.TIMESTAMP(timezone=False),server_default=func.current_timestamp(), nullable=False,comment="创建时间")
    created_by = db.Column(StringUUID, nullable=False,comment="创建人")


class MProdAttrValue(db.Model):
    """
    商品属性值
    """
    __tablename__ = "sp_prod_prop_val"
    __table_args__ = (
        db.PrimaryKeyConstraint("id", name="sp_prod_attr_value_pkey"),
        db.Index("sp_prod_attr_value_attr_id_idx", "prop_id"),
    )
    id = db.Column(db.BigInteger, primary_key=True, comment="id")
    prop_id = db.Column(db.BigInteger, nullable=False,comment="属性ID")
    val = db.Column(db.String(32), nullable=False,comment="属性值")
    created_at = db.Column(db.TIMESTAMP(timezone=False),server_default=func.current_timestamp(), nullable=False,comment="创建时间")
    created_by = db.Column(StringUUID, nullable=False,comment="创建人")


class MProdProperty(db.Model):
    """
    商品属性
    """
    __tablename__ = "sp_prod_prop"
    __table_args__ = (
        db.PrimaryKeyConstraint("id", name="sp_prod_prop_pkey"),
        db.Index("sp_prod_prop_shop_id_idx", "shop_id"),
    )
    id = db.Column(db.BigInteger, primary_key=True, autoincrement=True)
    name = db.Column(db.String(24), nullable=False,comment="属性名称")
    shop_id = db.Column(StringUUID, nullable=False,comment="店铺ID")
    is_sale_rule = db.Column(db.Boolean, nullable=False,server_default=db.text("false"),comment="是否销售规则属性")
    created_at = db.Column(db.TIMESTAMP(timezone=False),server_default=func.current_timestamp(), nullable=False,comment="创建时间")
    created_by = db.Column(StringUUID, nullable=False,comment="创建人")

    @property
    def prop_values(self) -> list[str]:
        return [v.val for v in db.session.query(MProdAttrValue).filter(MProdAttrValue.prop_id == self.id).all()]
